<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\CarOrderDetailDao;
use app\model\car\CarOrder;
use app\model\car\CarOrderDetail;
use app\services\BaseServices;
use think\exception\ValidateException;

class CarOrderDetailServices extends BaseServices
{
    public function __construct(CarOrderDetailDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page,$limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$page,$limit);
        foreach ($list['list'] as &$val){
            $val['voucher_pic'] = json_decode($val['voucher_pic'],true);
        }
        return $list;
    }

    /**
     * 创建当面收费
     * @param $data
     * @param $op_user
     * @param $op_user_type
     * @return string
     */
    public function singleFaceFee($data,$op_user,$op_user_type){
        $data['add_time'] = time();
        if(!CarOrderDetail::create($data)) throw new ValidateException('提交失败');
        $title = '创建当面收费【'.CarOrder::where('id',$data['order_id'])->value('order_no').'】收费内容【'.$data['fee_name'].'->'.$data['price'].'】';
        BaseLogServices::addLog($data['order_id'],2,$title,$op_user_type,$op_user['id'],$op_user['user_name'],13);
        return '提交成功';
    }

    /**
     * 扣款
     * @param $data
     * @param $op_user
     * @param $op_user_type
     * @return string
     */
    public function deductMoneyFee($data,$op_user,$op_user_type){
        $data['add_time'] = time();
        if(!CarOrderDetail::create($data)) throw new ValidateException('提交失败');
        $title = '订单【'.CarOrder::where('id',$data['order_id'])->value('order_no').'】扣款【'.$data['fee_name'].'->'.$data['price'].'】';
        BaseLogServices::addLog($data['order_id'],2,$title,$op_user_type,$op_user['id'],$op_user['user_name'],13);
        return '提交成功';
    }
}
